//
//  AMapService.m
//  HaoBaoRN
//
//  Created by Xiteng on 2017/5/15.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "AMapService.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <YYModel/YYModel.h>



typedef void(^CallBackBlock)(id result);
@interface AMapService()<AMapLocationManagerDelegate,AMapSearchDelegate >
@property(strong,nonatomic)AMapLocationManager* locationManager ;
@property(strong,nonatomic)AMapSearchAPI* search ;
@property(copy,nonatomic) CallBackBlock searchCallBack ;

@end


@implementation AMapService
RCT_EXPORT_MODULE();

- (instancetype)init
{
  self = [super init];
  if (self) {
    [self configLocationManager];
  }
  return self;
}


- (void)configLocationManager
{
  self.locationManager = [[AMapLocationManager alloc] init];
  
  [self.locationManager setDelegate:self];
  
  [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
  
  [self.locationManager setLocationTimeout:6];
  
  [self.locationManager setReGeocodeTimeout:3];
}

-(void)configSearchManager:(NSString *)keyword city:(NSString *)city callback:(CallBackBlock)callback
{
  self.searchCallBack = callback ;
  if(self.search==nil){
    self.search = [[AMapSearchAPI alloc] init];
  }
  self.search.delegate = self;
  
  AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
  
  request.keywords            = keyword;
  request.city                = city;
  request.requireExtension    = YES;
  
  /*  搜索SDK 3.2.0 中新增加的功能，只搜索本城市的POI。*/
  request.cityLimit           = YES;
  request.requireSubPOIs      = YES;
  
  [self.search AMapPOIKeywordsSearch:request];
}

-(void)configSearchManager:(CGFloat)la lt:(CGFloat)lt callback:(CallBackBlock)callback{
  self.searchCallBack = callback ;
  if(self.search==nil){
    self.search = [[AMapSearchAPI alloc] init];
  }
  self.search.delegate = self;
  
  AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
  request.location            = [AMapGeoPoint locationWithLatitude:la longitude:lt];
  /* 按照距离排序. */
  request.sortrule            = 0;
  request.requireExtension    = YES;
  [self.search AMapPOIAroundSearch:request];

}


/* POI 搜索回调. */
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
  if (response.count == 0)
  {
    return;
  }
  NSArray* pois = response.pois ;
  if (self.searchCallBack) {
    self.searchCallBack(@[[pois yy_modelToJSONObject]]);
    self.searchCallBack = nil ;
  }
}


#pragma mark - 地址解析
- (void)locateAction:(CallBackBlock)success failure:(CallBackBlock)fail
{
  //带逆地理的单次定位
  [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
    if (error)
    {
      if (error.code == AMapLocationErrorLocateFailed)
      {
        fail([error yy_modelToJSONObject]);
      }
    }
    //逆地理信息
    if (regeocode)
    {
      success([regeocode yy_modelToJSONObject]);
    }
  }];
}


RCT_REMAP_METHOD(locateEvent,
                 locationresolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  [self configLocationManager];
  [self locateAction:^(AMapLocationReGeocode* result) {
    resolve(result);
  } failure:^(NSError* err) {
    reject(@"500",@"",err);
  }];
}


#pragma mark - 关键字搜索
RCT_EXPORT_METHOD(searchAreaEvent:(NSString *)keyword city:(NSString *)city callback:(RCTResponseSenderBlock)callback)
{
  [self configSearchManager:keyword city:city callback:^(id result) {
    callback(result);
  }];
}


#pragma mark - 附近区域搜索
RCT_EXPORT_METHOD(searchEvent:(CGFloat)la lt:(CGFloat)lt callback:(RCTResponseSenderBlock)callback)
{
  [self configSearchManager:la lt:lt callback:^(id result) {
    callback(result);
  }];
}

- (dispatch_queue_t)methodQueue
{
  return dispatch_queue_create("com.facebook.React.AsyncLocalStorageQueue", DISPATCH_QUEUE_SERIAL);
}
@end
